package br.mil.eb.decex.ati.material;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;

public class MaterialDAOHibernate implements MaterialDAO {
	
	private Session sessao;

	@Override
	public void salvar(Material material) {
		this.sessao.save(material);

	}

	public Session getSessao() {
		return sessao;
	}

	public void setSessao(Session sessao) {
		this.sessao = sessao;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Material> listar() {
		Criteria lista = sessao.createCriteria(Material.class);
		return lista.list();
	}
	
	@Override
	public void excluir(Material material) {
		this.sessao.delete(material);
		
	}

	@Override
	public Material pesquisarPorDescricaO(String descricao) {String sql = "from Material m where m.descricao like :descricao";
		Query consulta = sessao.createQuery(sql);
		consulta.setString("descricao", "%"+descricao+"%");
		
		return (Material) consulta.uniqueResult();
	}	

	@Override
	public void alterar(Material material) {
		this.sessao.update(material);
		
	}

}
